<?php

namespace Viettrans\FrontBundle\Services;

class MyPaginator {
    public $countItems;
    public $countPages;
    public $countSegments;
    
    public $firstPage;
    public $lastPage;
    public $currentPage;
    public $nextPage;
    public $previousPage;

    public $firstSegment;
    public $lastSegment;
    public $currentSegment;
    public $nextSegment;
    public $previousSegment;
    
    public $maxItemsPerPage;
    public $maxPagesPerSegment;
    
    public $hasNextPage;
    public $hasPreviousPage;
    public $hasNextSegment;
    public $hasPreviousSegment;

    public $firstPageOfCurrentSegment;
    public $lastPageOfCurrentSegment;
    
    public function __construct() {}

    /**
     *
     * @param int $countItems
     * @param tyintpe $currentPage
     * @param int $maxItemsPerPage
     * @param int $maxPagesPerSegment 
     */
    public function build($countItems, $currentPage, $maxItemsPerPage = 10 , $maxPagesPerSegment = '') {
        # Get the value
        $this->countItems         = $countItems;
        $this->currentPage        = $currentPage;
        $this->maxItemsPerPage    = $maxItemsPerPage;
        $this->maxPagesPerSegment = $maxPagesPerSegment;
        
        # Get total page
        # Total page is always >=1, even when there are no results, we have always one page for show the error
        $this->countPages = ($countItems > 0) ? ceil($this->countItems/$this->maxItemsPerPage) : 1;
         
        # Get first page
        $this->firstPage = 1;
        
        # Get last page
        $this->lastPage = $this->countPages;

        # Clean current page
        if ($this->currentPage > $this->lastPage) $this->currentPage = $this->lastPage;
        if ($this->currentPage < $this->firstPage) $this->currentPage = $this->firstPage;
        
        # Check if has next page
        $this->hasNextPage = ($this->currentPage < $this->lastPage) ? true : false;
        
        # Check if has previous page
        $this->hasPreviousPage = ($this->currentPage > $this->firstPage) ? true : false;

		# Get next page
		$this->nextPage = ($this->hasNextPage) ? $this->currentPage + 1 : $this->lastPage;
		
		# Get previous page
		$this->previousPage = ($this->hasPreviousPage) ? $this->currentPage - 1 : 1;
        
        if (!empty($maxPagesPerSegment)) {
			# Get current segment
			$this->currentSegment = ceil($this->currentPage/$this->maxPagesPerSegment);
		
            # Get total segment
            $this->countSegments = ceil($this->countPages/$this->maxPagesPerSegment);
           
            # Get first segment
            $this->firstSegment = 1;
            
            # Get last segment
            $this->lastSegment = $this->countSegments;
            
            # Check if has next segment
            $this->hasNextSegment = ($this->currentSegment < $this->lastSegment) ? true : false;
            
            # Check if has previous segment
            $this->hasPreviousSegment = ($this->currentSegment > $this->firstSegment) ? true : false;
            
            # Get first page of segment
            $this->firstPageOfCurrentSegment = ($this->currentSegment - 1)*$this->maxPagesPerSegment + 1;
            
            # Get last page of segment
            if (($this->lastPage - $this->firstPageOfCurrentSegment) <= $this->maxPagesPerSegment) {
                $this->lastPageOfCurrentSegment = $this->lastPage;
            } else {
                $this->lastPageOfCurrentSegment = ($this->currentSegment*$this->maxPagesPerSegment);
            }
        }

        return $this;
    }
}

?>
